﻿using System;
using System.Collections.Generic;
using Giraffe.Core.Common.Messengers;

namespace Giraffe.Core.ICQ
{
    public class IcqMessenger : IMessenger
    {
        private readonly IcqConfiguration _configuration;
        private readonly IcqSession _session;
        private readonly IcqContactsManager _contactsManager;
        private readonly IcqConversationsManager _coversationsManager;

        public event Action Logined;
        public event Action ContactListChanged;
        public event Action<IMessengerContact> NewConversationRecieved;

        public IcqMessenger(IcqConfiguration configuration)
        {
            this._configuration = configuration;
            _session = new IcqSession();
            _contactsManager = new IcqContactsManager(_session, configuration.Login);
            _contactsManager.ContactChanged += OnContactChanged;
            _contactsManager.ContactListChanged += OnContactListChanged;
            _coversationsManager = new IcqConversationsManager(_session, _contactsManager);
            _coversationsManager.NewConversationStarted += OnNewConversationStarted;

        }

        #region IMessenger members

        public MessengerType MessengerType
        {
            get { return MessengerType.Icq; }
        }

        public IEnumerable<IMessengerContact> ContactList
        {
            get
            {
                return _contactsManager.ContactList;
            }
        }

        public IMessengerContact OwnContact
        {
            get
            {
                return _contactsManager.OwnContact;
            }
        }

        public virtual void Login()
        {
            _session.StateChanged += OnStateChanged;
            _session.Login(_configuration.Login, _configuration.Password);
        }

        public void Logout()
        {
            _session.Logout();
        }

        public bool IsLogined
        {
            get { return _session.State == MessengerState.Online; }
        }

        public IMessengerConversation StartConversation(IMessengerContact companion)
        {
            return _coversationsManager.GetSingleConversation(companion);
        }

        #endregion

        private void OnStateChanged(MessengerState state)
        {
            switch (state)
            {
                case MessengerState.Online:
                    if (Logined != null)
                    {
                        _contactsManager.UpdateContacts();
                        Logined();
                    }
                    break;
            }
        }

        private void OnContactListChanged()
        {
            if (ContactListChanged != null)
            {
                ContactListChanged();
            }
        }

        private void OnContactChanged(IMessengerContact obj)
        {
            if (ContactListChanged != null)
            {
                ContactListChanged();
            }
        }

        private void OnNewConversationStarted(IMessengerContact contact)
        {
            NewConversationRecieved(contact);
        }
    }
}
